package com.example.springboottools.utils.image;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.io.FileHandler;

import java.io.File;
import java.io.IOException;

public class MhtmlToImageConverter {
    public static void main(String[] args) {
        // 设置ChromeDriver路径
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\SH102909\\Downloads\\chromedriver-win64\\chromedriver.exe");

        // 创建ChromeOptions对象
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--headless"); // 无头模式
        options.addArguments("--disable-gpu");
        options.addArguments("--window-size=1920,1080");

        // 创建WebDriver对象
        WebDriver driver = new ChromeDriver(options);

        try {
            // 加载MHTML文件
            String mhtmlFilePath = "D:\\新增商品\\图片组织示意图-全包.mhtml";
            String mhtmlUrl = "file://" + mhtmlFilePath;
            driver.get(mhtmlUrl);

            // 截图并保存为图片
            File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
            FileHandler.copy(screenshot, new File("D:\\新增商品\\image.png"));
            System.out.println("MHTML文件已成功转换为图片");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭浏览器
            driver.quit();
        }
    }
}
